require "dgo/tile_map/collision_type"
module MapEditor
  module PaletControler
    class PaletBaseSetter
      include DGO::TileMap
      def initialize
        @textures = {}
        @textures[:circle] = Texture.load($res.tool_path + "res/circle")
        @textures[:cross] = Texture.load($res.tool_path + "res/cross")
        @textures[:star] = Texture.load($res.tool_path + "res/star")
      end
      
      def target=(target)
        @target = target
      end
    
      def on_left_down(e)
        @target.sx = ((e.x + @target.scroll_x) / @target.zoomed_grid_width).floor
        @target.sy = ((e.y + @target.scroll_y)/ @target.zoomed_grid_height).floor

        palet_chip = @target.get_palet_chip(@target.sx, @target.sy)

        return if palet_chip.readonly?
        
        dx = (e.x + @target.scroll_x) % @target.zoomed_grid_width
        dy = (e.y + @target.scroll_y) % @target.zoomed_grid_height

        case(self.get_palet_chip_state(palet_chip))
          when :circle
            palet_chip.collision_type = CollisionType::ALL
          when :cross
            palet_chip.priority = 1
            palet_chip.collision_type = CollisionType::NONE
          when :star
            palet_chip.priority = 0
            palet_chip.collision_type = CollisionType::NONE
        end

        @target.render
      end
      
      def get_palet_chip_state(palet_chip)
        if palet_chip.priority == 1
          return :star
        else
          if palet_chip.collision_type == CollisionType::ALL
            return :cross
          else
            return :circle
          end
        end
      end
      
      def on_left_up(e)

      end

      def on_right_down(e)

      end

      def on_right_up(e)
      end

      def on_motion(e)
      end
      
      def on_double_clicked(e)
      
      end
      
      def render(s)
      @target.render_grid(s, @target.grid_width, @target.grid_height)
      
      (@target.scroll_x_count..@target.right_count - 1).each do |i|
        (@target.scroll_y_count..@target.bottom_count - 1).each do |j|
            t = @target.get_palet_chip(i, j)

            tx = i - @target.scroll_x_count
            ty = j - @target.scroll_y_count

            s.render_texture(@textures[self.get_palet_chip_state(t)], tx * @target.grid_width, ty * @target.grid_height, :alpha => 200)
          end
        end
      end
      
      def render_detail(s)
      
      end
    end
  end
end